﻿using SqlSugar;
using Zwt.System.Interfaces.IService;
using Zwt.System.Model;

namespace Zwt.System.Interfaces.Service
{
    public class SysUsersService : BaseService<Sys_Users>, ISysUsersService
    {

        public SysUsersService(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }

        #region CustomInterface 

        /// <summary>
        /// 获取用户权限标识
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<string> GetUserPowers(string userId)
        {
            // 超级管理员拥有所有权限
            if (userId == "admin")
            {
                return Db.Queryable<Sys_Power>().Select(m => m.Name).ToList();
            }

            return Db.Queryable<Sys_UserRole, Sys_RolePower, Sys_Power>
                ((a, b, c) => new object[] { JoinType.Inner, a.RoleID == b.RoleUID, JoinType.Inner, b.PowerUID == c.ID })
                .Where((a, b, c) => a.UserID == userId)
                .Select((a, b, c) => c.Name).ToList();
        }

        /// <summary>
        /// 获取用户拥有的数据权限列表
        /// </summary>
        /// <returns></returns>
        public List<Sys_UserRelation> GetUserRelation(string userId)
        {
            return Db.Queryable<Sys_UserRelation>().Where(m => m.UserID == userId).ToList();
        }

        #endregion

    }
}
